<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

require_once 'Classes/Controllers/Controller.php';
require_once 'Classes/Model/ArticleModel.php';
require_once 'Classes/Model/FamilyModel.php';



class ArticlesController Extends Controller {

    
        public function processRequest() {
        switch (HTTPRequestModel::getInstance()->extractGETValue('action')){
            case 'AddArticle':
                $this->addArticle();
                break;
            case 'EditArticle':
                $this->editArticle();
                break;
            case 'DeleteArticle':
                $this->deleteArticle();
                break;
            case 'ListAllArticles':
                $this->listArticlesFromAllSubFamilies();
                break;
            default:
                $this->listArticles();
                break;
        }
    }
    
    
    public function listArticlesFromAllSubFamilies(){
         if ( strtolower(HTTPRequestModel::getInstance()->getRequest()) == "get"){
            
            require_once 'Classes/View/Pages/ListBasedPage.php';

            try{
                $subfamilyID = HTTPRequestModel::getInstance()->extractGETValue('subfamily');
                $articles = ArticleModel::getInstance()->getAllArticles();
            }
            catch(NoObjectInDatabaseException $e){
                $articles = null;
            }
            $title = HTTPRequestModel::getInstance()->extractGETValue("subFamily");
            $this->view = new ListBasedPage($title);
            $this->view->addCSS('/Ressources/style.css');
            $this->view->addElement($articles);
            $this->view->display();
        }
    }
    
    
    public function addArticle(){
        
         if(strtolower(HTTPRequestModel::getInstance()->getRequest()) == "post"){
             
             $familyId =HTTPRequestModel::getInstance()->extractGETValue('family');
             $subfamilyId =HTTPRequestModel::getInstance()->extractGETValue('subfamily');

             
             $array =array();
             $array['ID']="";
             $array['Name'] = HTTPRequestModel::getInstance()->extractPOSTValue('Title');
             $array['Description'] = HTTPRequestModel::getInstance()->extractPOSTValue('Description');
             $array['BarCode'] = HTTPRequestModel::getInstance()->extractPOSTValue('BarCode');
             $array['Statut'] = HTTPRequestModel::getInstance()->extractPOSTValue('Status');
             $array['Sub_FamilyID']= HTTPRequestModel::getInstance()->extractPOSTValue('Subfamily');
             $array['StructureID'] = HTTPRequestModel::getInstance()->extractPOSTValue('Structure');
             $array['Optional_1'] = HTTPRequestModel::getInstance()->extractPOSTValue('Opt1');
             $array['Optional_2'] = HTTPRequestModel::getInstance()->extractPOSTValue('Opt2');
             $array['Optional_3'] = HTTPRequestModel::getInstance()->extractPOSTValue('Opt3');
             $array['Optional_4'] = HTTPRequestModel::getInstance()->extractPOSTValue('Opt4');
             $array['Optional_5'] = HTTPRequestModel::getInstance()->extractPOSTValue('Opt5');
             $array['Optional_6'] = HTTPRequestModel::getInstance()->extractPOSTValue('Opt6');
             $array['LoanID'] = HTTPRequestModel::getInstance()->extractPOSTValue('Loans');
             
             
             $article = new Article($array);
             
             ArticleModel::getInstance()->addArticle($article);
             
             header('Location: /'.$familyId.'/'.$subfamilyId.'/');
             
             
         }
         else if(strtolower(HTTPRequestModel::getInstance()->getRequest()) == "get") {
            require_once 'Classes/View/Pages/FormBasedPage.php';

            $this->view = new FormBasedPage("New Article");
            $this->view->addCSS('/Ressources/style.css');
            $this->view->addElement(new Article());
            $this->view->display();
         }
        
    }
    
    public function deleteArticle(){
        
         $articleId =HTTPRequestModel::getInstance()->extractGETValue('article');
         $subfamilyId =HTTPRequestModel::getInstance()->extractGETValue('subfamily');
         $familyId =HTTPRequestModel::getInstance()->extractGETValue('family');

         ArticleModel::getInstance()->deleteArticlesById($articleId);
         
         
         header('Location: /'.$familyId.'/'.$subfamilyId.'/');
    }
    
    public function editArticle(){
        
         if(strtolower(HTTPRequestModel::getInstance()->getRequest()) == "post"){
            $array['ID']=HTTPRequestModel::getInstance()->extractGETValue('article');
             $array['Name'] = HTTPRequestModel::getInstance()->extractPOSTValue('Title');
             $array['Description'] = HTTPRequestModel::getInstance()->extractPOSTValue('Description');
             $array['BarCode'] = HTTPRequestModel::getInstance()->extractPOSTValue('BarCode');
             $array['Statut'] = HTTPRequestModel::getInstance()->extractPOSTValue('Status');
             $array['Sub_FamilyID']= HTTPRequestModel::getInstance()->extractPOSTValue('Subfamily');
             $array['StructureID'] = HTTPRequestModel::getInstance()->extractPOSTValue('Structure');
             $array['Optional_1'] = HTTPRequestModel::getInstance()->extractPOSTValue('Opt1');
             $array['Optional_2'] = HTTPRequestModel::getInstance()->extractPOSTValue('Opt2');
             $array['Optional_3'] = HTTPRequestModel::getInstance()->extractPOSTValue('Opt3');
             $array['Optional_4'] = HTTPRequestModel::getInstance()->extractPOSTValue('Opt4');
             $array['Optional_5'] = HTTPRequestModel::getInstance()->extractPOSTValue('Opt5');
             $array['Optional_6'] = HTTPRequestModel::getInstance()->extractPOSTValue('Opt6');
             $array['LoanID'] = HTTPRequestModel::getInstance()->extractPOSTValue('Loans');
             
             $subfamilyId =HTTPRequestModel::getInstance()->extractGETValue('subfamily');
             $familyId =HTTPRequestModel::getInstance()->extractGETValue('family');
            
            $article = new Article($array);
            
            $error=0;
           // faire la vérification
            
            if($error ==0){
                
                ArticleModel::getInstance()->updateArticle($article);
                header('Location: /'.$familyId.'/'.$subfamilyId.'/');
            }
            else {
                
                require_once 'Classes/View/Pages/FormBasedPage.php';

                
                $this->view = new FormBasedPage("Edit Article");
                $this->view->addCSS('/Ressources/style.css');
                $articleId = HTTPRequestModel::getInstance()->extractGETValue('article');
                $article = ArticleModel::getInstance()->getArticlesById($articleId);
                $this->view->addElement($article);
                $this->view->display();
                
            }
            
            
            
         }
         else if(strtolower(HTTPRequestModel::getInstance()->getRequest()) == "get") {
            require_once 'Classes/View/Pages/FormBasedPage.php';
            
            
            $this->view = new FormBasedPage("Edit Article");
            $this->view->addCSS('/Ressources/style.css');
            $articleId = HTTPRequestModel::getInstance()->extractGETValue('article');
            $article = ArticleModel::getInstance()->getArticlesById($articleId);
            $this->view->addElement($article);
            $this->view->display();
        }
    }
    
    public function listArticles(){
        
        if ( strtolower(HTTPRequestModel::getInstance()->getRequest()) == "get"){
            
            require_once 'Classes/View/Pages/ListBasedPage.php';

            try{
                $subfamilyID = HTTPRequestModel::getInstance()->extractGETValue('subfamily');
                $articles = ArticleModel::getInstance()->getArticlesBySubFamilyId($subfamilyID);
            }
            catch(NoObjectInDatabaseException $e){
                $articles = null;
            }
            $title = HTTPRequestModel::getInstance()->extractGETValue("subFamily");
            $this->view = new ListBasedPage($title);
            $this->view->addCSS('/Ressources/style.css');
            $this->view->addElement($articles);
            $this->view->display();
        }
    }
    
    
    
}

?>
